using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Magiq.Delete;
using Magiq.Insert;
using Magiq.Update;

namespace Magiq {
    public static class Enumerable {
        public static IUpdate<T> Set<T, R>(this IEnumerable<T> enumerable, Expression<Func<T, R>> property, R value)
            where T : class {
            return new EnumerableUpdate<T>(enumerable).Set(property, value);
        }

        public static IUpdate<T> Set<T, R>(this IEnumerable<T> enumerable, Expression<Func<T, R>> property,
                                           Expression<Func<T, R>> value) where T : class {
            return new EnumerableUpdate<T>(enumerable).Set(property, value);
        }

        public static int Delete<T>(this IEnumerable<T> enumerable) where T : class {
            return new EnumerableDelete<T>(enumerable).Execute();
        }

        public static int Delete<T>(this IEnumerable<T> enumerable, Func<T, bool> where) where T : class {
            return enumerable.Where(where).Delete();
        }

        public static IInsert Insert<F, T>(this IEnumerable<F> enumerable, Expression<Func<F, T>> select)
            where F : class
            where T : class {
            return new EnumerableInsert<F, T>(enumerable, select);
        }

        public static IQueryable<T> ToQueryable<T>(this IEnumerable<T> enumerable) where T : class {
            if (enumerable is IQueryable<T>)
                return (IQueryable<T>) enumerable;

            return Magiq.For(enumerable).CreateQueryableFrom(enumerable);
        }
    }
}